﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using dreamwork.pay.api.Infrastructure.EFSqlServer;
using dreamwork.pay.api.Model.Data;
using dreamwork.pay.api.Services;
using Dreamwork.Core.Infrastructure.RDSDb;
using DreamWork.Web.API.Infrastructure;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;

namespace dreamwork.pay.api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
			services.AddMvc()
				 .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
				 .AddControllersAsServices();


			#region SQLDB Infracture
			services.AddDbContext<PayDbContext>(options =>
			{
				options.UseSqlServer(Configuration["SqlServer:ConnectionString"], sqlOptions =>
				{
					//sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
					sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
				});
			});
			services.AddTransient<IRDSRepository<PayUser, Guid>, PayUserRepository>();
			services.AddTransient<IRDSRepository<Provider, Guid>, ProviderRepository>();
			#endregion

			#region Domain Services
			services.AddTransient<IPayServices, PayServices>();
			#endregion

			services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
			  .AddIdentityServerAuthentication(options =>
			  {
				  options.Authority = "http://localhost:5200";
				  options.RequireHttpsMetadata = false;
				  options.ApiName = "dreamwork.pay.api";
			  });

			#region Swagger
			services.AddSwaggerGen(options =>
			{
				options.DescribeAllEnumsAsStrings();
				options.SwaggerDoc("v1", new Info { Title = "Dreamwork Pay API", Version = "v1" });
				options.OperationFilter<HttpAuthHeaderFilter>();
			});
			#endregion

			#region Cors
			services.AddCors(options =>
			{
				options.AddPolicy("CorsPolicy",
					builder => builder.AllowAnyOrigin()
					.AllowAnyMethod()
					.AllowAnyHeader()
					.AllowCredentials());
			});
			#endregion
		}

		// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
		public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
			if (env.IsDevelopment())
			{
				app.UseDeveloperExceptionPage();
			}
			else
			{
				app.UseHsts();
			}
			app.UseMvc();
			app.UseCors("CorsPolicy");
			app.UseSwagger();
			app.UseAuthentication();
			app.UseHttpsRedirection();
			app.UseMvcWithDefaultRoute();
			app.UseStaticFiles();
			app.UseSwagger()
			   .UseSwaggerUI(c =>
			   {
				   c.SwaggerEndpoint("/swagger/v1/swagger.json", "Dreamwork.Pay.API");
			   });
		}
    }
}
